<template>
    <div>
        <h1>登陆</h1>
        <p>用户名： <input type="text" v-model="username"></p>
        <p>密码： <input type="password" v-model="password"></p>
        <p>
            <button @click="handleLogin">登陆</button>
        </p>
    </div>
</template>

<script>
    import axios from 'axios'

    export default {
        name: "LoginView",
        data() {
            return {
                username: '',
                password: '',
            }
        },
        methods: {
            handleLogin() {
                axios.post('http://127.0.0.1:8000/api/login/',
                    {
                        username: this.username,
                        password: this.password
                    })
                    .then(res => {
                        if (res.data.code === 100) {
                            // 跳转 首页
                            // this.$router.push('/')
                            const token = res.data.token;
                            // 存储 token
                            sessionStorage.setItem('token', token);
                            sessionStorage.setItem('username', this.username);
                            this.$router.push('/Userinform')
                        } else {
                            this.$message({
                            type: 'info',
                            message: '登陆失败',
                        });
                        }
                    }).catch(()=>{
                         this.$message({
                            type: 'info',
                            message: '登陆失败',
                        });
                })
            }
        }

    }
</script>

<style scoped>


</style>